package com.heima.file.service.impl;

import cn.hutool.core.util.StrUtil;
import com.heima.file.config.MinIOConfigProperties;
import com.heima.file.service.FileStorageService;
import io.minio.MinioClient;
import io.minio.ObjectWriteResponse;
import io.minio.PutObjectArgs;
import io.minio.errors.*;

import java.io.IOException;
import java.io.InputStream;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class MinIOFileStorageServiceImpl implements FileStorageService {

    private MinioClient minioClient;
    private MinIOConfigProperties minIOConfigProperties;

    public MinIOFileStorageServiceImpl(MinioClient minioClient, MinIOConfigProperties minIOConfigProperties) {
        this.minioClient = minioClient;
        this.minIOConfigProperties = minIOConfigProperties;
    }

    private final static String separator = "/";
    /**
     * @param dirPath  文件目录
     * @param filename yyyy/mm/dd/file.jpg
     */
    public String builderFilePath(String dirPath, String filename) {
        StringBuilder stringBuilder = new StringBuilder(50);
        if (StrUtil.isNotEmpty(dirPath)) {
            stringBuilder.append(dirPath).append(separator);
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
        String todayStr = sdf.format(new Date());
        stringBuilder.append(todayStr).append(separator);
        stringBuilder.append(filename);
        return stringBuilder.toString();
    }

    @Override
    public String uploadImgFile(String prefix, String filename, InputStream inputStream) {
        String filePath = builderFilePath(prefix, filename);
        try {
            PutObjectArgs putObjectArgs = PutObjectArgs.builder()
                    .object(filePath)//文件名
    //                    .contentType("text/html")//文件类型
                    .contentType("image/" + StrUtil.subAfter(filename, ".", true))//文件类型
                    .bucket(minIOConfigProperties.getBucket())//桶名词  与minio创建的名词一致
                    .stream(inputStream, inputStream.available(), -1) //文件流
                    .build();

            minioClient.putObject(putObjectArgs);
            return StrUtil.format("{}{}{}{}{}", minIOConfigProperties.getReadPath(),
                    "/",
                    minIOConfigProperties.getBucket(),
                    "/",
                    filePath);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public String uploadHtmlFile(String prefix, String filename, InputStream inputStream) {
        String filePath = builderFilePath(prefix, filename);
        try {
            PutObjectArgs putObjectArgs = PutObjectArgs.builder()
                    .object(filePath)//文件名
                    //                    .contentType("text/html")//文件类型
                    .contentType("text/html")//文件类型
                    .bucket(minIOConfigProperties.getBucket())//桶名词  与minio创建的名词一致
                    .stream(inputStream, inputStream.available(), -1) //文件流
                    .build();

            minioClient.putObject(putObjectArgs);
            return StrUtil.format("{}{}{}{}{}", minIOConfigProperties.getReadPath(),
                    "/",
                    minIOConfigProperties.getBucket(),
                    "/",
                    filePath);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void delete(String pathUrl) {

    }

    @Override
    public byte[] downLoadFile(String pathUrl) {
        return new byte[0];
    }
}
